package blue.blog.core.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.blue.jdbc.BaseDao;

import blue.blog.core.model.CmsDocFile;


@Repository
public class CmsDocFileDao extends BaseDao<CmsDocFile>
{
	public CmsDocFileDao()
	{
	}

	@Override
	protected void query(StringBuilder sql, CmsDocFile param)
	{
		if (param != null)
		{
			if (param.getDocId() != null && param.getDocId() != -1)
				sql.append(" and a.doc_id=:docId");

			if (param.getState() != null && param.getState() != -1)
				sql.append(" and a.state=:state");
			
		}
		sql.append(" order by a.id");
	}

	public void deleteByDoc(Integer docId)
	{
		String sql = "update cms_doc_file set doc_id=null where doc_id=?";
		jdbcTemplate.update(sql, docId);
	}
	
	public List<CmsDocFile> listByDoc(Integer docId)
	{
		Map<String, Object> param = new HashMap<>();
		param.put("docId", docId);
		String sql = "select * from cms_doc_file where doc_id=:docId";
		return jdbcObjectTemplate.list(CmsDocFile.class, sql, param);
	}
	
	public boolean existPath(String path)
	{
		String sql = "select count(*) from cms_doc_file where path=?";
		int n = jdbcTemplate.queryForObject(sql, Integer.class, path);
		return n > 0;
		
	}
	
}
